<?php
class Csv_to_database extends CI_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('Export_csv_m');
    }
    public function index()
    {
        $source_csv=base_url().'csv_source/file.csv';
        $handle = fopen($source_csv, 'r');
        $id_question = 0;
        while ($data= fgetcsv($handle,","))
        {
            
            if($data[0]=="Q(1)"){
                if($data[1]=="QS") { $qs = $data[2]; $id_question++;}
                else if($data[1]=="S(1)") $s1 = $data[2];
                    else if($data[1]=="S(2)") $s2 = $data[2];
                        else if($data[1]=="S(3)") $s3 = $data[2];
                            else if($data[1]=="S(4)") $s4 = $data[2];
                                else if($data[1]=="S(5)") $s5 = $data[2];
                                    else if($data[1]=="S(6)") $s6 = $data[2];
                                        else if($data[1]=="AN(1)") $an_ks = $data[3];
                else if($data[1]=="SC") $sc = $data[3];
                if(isset($sc))
                {
                    echo "insert bang 1</br>";
                    $this->Export_csv_m->insert_to_q1($id_question,$qs,$s1,$s2,$s3,$s4,$s5,$s6,$an_ks,$sc);
                    $this->Export_csv_m->insert_to_compare("q1",$id_question);
                    $qs=NULL; $s1=NUll;$s2=NULL;$s3=NULL;$s4=NULL;$s5=NULL;$s6=NULL;$an_ks=NULL;$sc=NULL;
                }
            }

            else if($data[0]=="Q(2)"){
                if($data[1]=="QS") { $qs = $data[2];$id_question++;}
                    else if($data[1]=="S(1)") $s1 = $data[2];
                        else if($data[1]=="S(2)") $s2 = $data[2];
                            else if($data[1]=="S(3)") $s3 = $data[2];
                                else if($data[1]=="S(4)") $s4 = $data[2];
                                    else if($data[1]=="S(5)") $s5 = $data[2];
                                        else if($data[1]=="S(6)") $s6 = $data[2];
                                            else if($data[1]=="AN(1)") $an_kso = $data[3].",".$data[4];
                                                else if($data[1]=="SC(1)") $sc = $data[3];
                if(isset($sc))
                {
                    echo "inset bang 2</br>";
                    $this->Export_csv_m->insert_to_q2($id_question,$qs,$s1,$s2,$s3,$s4,$s5,$s6,$an_kso,$sc);
                    $this->Export_csv_m->insert_to_compare("q2",$id_question);
                    $qs=NULL; $s1=NUll;$s2=NULL;$s3=NULL;$s4=NULL;$s5=NULL;$s6=NULL;$an_ks=NULL;$sc=NULL;
                }
            }
                
            else if($data[0]=="Q(3)"){
                if($data[1]=="QW") { $qw = $data[2]; $id_question++;}
                    else if($data[1]=="WR(1)") $wr1 = $data[2];
                        else if($data[1]=="AN(1)") $an1_kw = $data[3];
                            else if($data[1]=="AN(2)") $an2_kw = $data[3];
                                else if($data[1]=="SC"&&$data[2]=="AN(1)") $sc_an1 = $data[3];
                                    else if($data[1]=="SC"&&$data[2]=="AN(2)") $sc_an2 = $data[3];
                if(isset($sc_an2))
                {
                    echo "insert bang b3</br>";
                    $this->Export_csv_m->insert_to_q3($id_question,$qw,$wr1,$an1_kw,$an2_kw,$sc_an1,$sc_an2);
                    $this->Export_csv_m->insert_to_compare("q3",$id_question);
                    $qw=NULL;$wr1=NULL;$an1_kw=NULL;$an2_kw=NULL;$sc_an1=NULL;$sc_an2=NULL;
                }
            }
            
            else if($data[0]=="Q(4)"){
                
                if($data[1]=="QS") {$qs = $data[2]; $id_question++;}
                    else if($data[1]=="S(1)") {$s1=$data[2];}//echo $s1;echo "</br>";}
                        else if($data[1]=="S(2)") {$s2=$data[2];}//echo $s2;echo "</br>";}
                            else if($data[1]=="S(3)") {$s3=$data[2];}//echo $s3;echo "</br>";}
                                else if($data[1]=="S(4)") {$s4=$data[2];}// echo $s4;echo "</br>";}
                                    else if($data[1]=="S(5)") {$s5=$data[2];}// echo $s5;echo "</br>";}
                                        else if($data[1]=="S(6)") {$s6=$data[2];}// echo $s6;echo "</br>";}
                                            else if($data[1]=="S(7)") {$s7=$data[2];}// echo $s7;echo "</br>";}
                                                else if($data[1]=="AN(1)") {$an1_ks=$data[3]; }//echo $an1_ks;echo "</br>";}
                                                    else if($data[1]=="AN(2)") {$an2_ks=$data[3];}// echo $an2_ks;echo "</br>";}
                                                        else if($data[1]=="AN(3)") {$an3_ksa=$data[3].",".$data[4];}// echo $an3_ksa;echo "</br>";}
                                                            else if($data[1]=="AN(4)") {$an4_kso=$data[3].",".$data[4].",".$data[5].",".$data[6].",".$data[7];}// echo $an4_kso;echo "</br>";}
                                                                else if($data[1]=="SC(1)") {$sc1=$data[3];}// echo $sc1;echo "</br>";}
                                                                    else if($data[1]=="SC(2)") {$sc2=$data[3];}// echo $sc2;echo "</br>";}
                                                                        else if($data[1]=="SC(3)") {$sc3=$data[3];}// echo $sc3;echo "</br>";}
                                                                            else if($data[1]=="SC(4)") {$sc4=$data[3];}// echo $sc4;echo "</br>";}
                if(isset($sc4))
                {
                    echo "insert bang b4</br>";
                    $this->Export_csv_m->insert_to_q4($id_question,$qs,$s1,$s2,$s3,$s4,$s5,$s6,$s7,
                                    $an1_ks,$an2_ks,$an3_ksa,$an4_kso,$sc1,$sc2,$sc3,$sc4);
                    $this->Export_csv_m->insert_to_compare("q4",$id_question);
                    $qs=NULL;$s1=NULL;$s2=NULL;$s3=NULL;
                    $s4=NULL;$s5=NULL;$s6=NULL;$s7=NULL;
                    $an1_ks=NULL;$an2_ks=NULL;$an3_ksa=NULL;$an4_kso=NULL;
                    $sc1=NULL;$sc2=NULL;$sc3=NULL;$sc4=NULL;
                }
            }
            
            else if($data[0]=="Q(5)"){
                if($data[1]=="QW") {$qw = $data[2]; $id_question++;}
                    else if($data[1]=="WR(1)") $wr1 = $data[2];
                        else if($data[1]=="INS") $ins = $data[2];
                            else if($data[1]=="SC(1)") $sc = $data[2];
                if(isset($sc)){
                    echo "insert bang 5</br>";
                    $this->Export_csv_m->insert_to_q5($id_question,$qw,$wr1,$ins,$sc);
                    $this->Export_csv_m->insert_to_compare("q5",$id_question);
                    $qw=NULL;$wr1=NULL;$ins=NULL;$sc=NULL;
                }
                
            }
            
            else if($data[0]=="Q(6)"){
                
                
                if($data[1]=="QW") { $qw = $data[2];$id_question++;}
                    else if($data[1]=="WR(1)") {$wr1 = $data[2]; }//echo $wr1;}
                        else if($data[1]=="WR(2)") {$wr2 = $data[2]; }
                            else if($data[1]=="WR(3)") {$wr3 = $data[2]; }//echo $wr3;}
                                else if($data[1]=="AN(1)") {$an1_kwpo = $data[3].",".$data[4]; }//echo $an1_kwpo;}
                                    else if($data[1]=="AN(2)") {$an2_kwpo = $data[3].",".$data[4]; }//echo $an2_kwpo."</br>";}
                                        else if($data[1]=="AN(3)") {$an3_kwpo = $data[3].",".$data[4]; }//echo $an3_kwpo."</br>";}
                                            else if($data[1]=="AN(4)") {$an4_kwpo = $data[3].",".$data[4]; }//echo $an4_kwpo."</br>";}
                                                else if($data[1]=="INS") {$ins = $data[2]; }//echo $ins."</br>";}
                                                    else if($data[1]=="SC(1)") {$sc1 = $data[3]; }//echo $sc1."</br>";}
                                                        else if($data[1]=="SC(2)") {$sc2 = $data[3]; }//echo $sc2."</br>";}
                                                            else if($data[1]=="SC(3)") {$sc3 = $data[3]; }//echo $sc3."</br>";}
                                                                else if($data[1]=="SC(4)") {$sc4 = $data[3]; }//echo $sc4."</br>";}
                if(isset($sc4))
                {   
                   $this->Export_csv_m->insert_to_q6($id_question,$qw,$wr1,$wr2,$wr3,$an1_kwpo,
                                            $an2_kwpo,$an3_kwpo,$an4_kwpo,$ins,$sc1,$sc2,$sc3,$sc4);
                   $this->Export_csv_m->insert_to_compare("q6",$id_question);
                    $qw=NULL; $wr1=NUll;$wr2=NULL;$wr3=NULL;
                    $an1_kwpo=NULL;$an2_kwpo=NULL;$an3_kwpo=NULL;$an4_kwpo=NULL;
                    $ins=NULL;$sc1=NULL;$sc2=NULL;$sc3=NULL;$sc4=NULL;
                   
                }
                
                
                
            }
             
 
         }
            
            
        
        
        fclose($handle);
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
